package com.sky.service.impl;

import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.ReportService;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ReportServiceImpl implements ReportService {

     @Autowired
     private UserMapper userMapper;

    @Autowired
    private OrderMapper orderMapper;
    /**
     * 统计指定时间区间内的数据
     * @param begin
     * @param end
     * @return
     */
    @Override
    public TurnoverReportVO getTurnoverStatistics(LocalDate begin, LocalDate end) {
        //日期,以逗号分隔,例如:2022-10-01,2022-10-02

        //当前集合用于存放begin到end的日期
        List<LocalDate> dateList=new ArrayList<>();

        //
        while (!begin.equals(end)){//不等于的时候 一天一天加下去 结果为false则 跳出循环
            //计算指定日期的后一天
            begin=begin.plusDays(1);
            dateList.add(begin);
        }
        //存放每天的营业额
        List<Double> turnoverList=new ArrayList<>();
        for (LocalDate date : dateList) {
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);

            Map map=new HashMap<>();
            map.put("begin",beginTime);
            map.put("end",endTime);
            map.put("status", Orders.COMPLETED);


            //select sum(amount) where orderTime> and orderTime< and status=5
            Double turnover= orderMapper.sumByMap(map);

            //如果一天或这几天0收益额怎么办 这时候turnover是null我们用三目运算符转换成空
            turnover=turnover==null ? 0.0:turnover;
            turnoverList.add(turnover);
        }

        //使用工具类 将日期集合遍历 并且 按 , 分割开来 并转换成字符串格式
        String join = StringUtils.join(dateList, ",");
        TurnoverReportVO turnoverReportVO=TurnoverReportVO.builder()
                .dateList(join)
                .turnoverList(StringUtils.join(turnoverList,","))
                .build();


        return turnoverReportVO ;
    }

    /**
     * 统计每日用户和每日新增用户
     * @param begin
     * @param end
     * @return
     */
    @Override
    public UserReportVO getUserStatistics(LocalDate begin, LocalDate end) {
        //统计每日用户 其实是统计已完成的单数有多少单
        List<LocalDate> dateList=new ArrayList<>();
        //先加begin
        dateList.add(begin);

        while (!begin.equals(end)){
            begin=begin.plusDays(1);
            dateList.add(begin);
        }
        //存放每天新增用户数量和每天的总用户数量 select count(id) from user where creat_time<? and create_time>?
        List<Integer> newUserList=new ArrayList<>();
        //select count(id) from user where create_Time<?
        List<Integer> totalUserList=new ArrayList<>();

        //得将LocalDate 转换成LocalDateTime
        for (LocalDate localDate : dateList) {
            LocalDateTime beginDateTime=LocalDateTime.of( localDate,LocalTime.MIN);
            LocalDateTime endDateTime=LocalDateTime.of( localDate,LocalTime.MAX);
            Map map=new HashMap<>();
            map.put("end",endDateTime);
            //总用户数量
            Integer countTotalMapper = userMapper.countByMap(map);
            map.put("begin",beginDateTime);
            //新增用户数量
            Integer newUserMapper = userMapper.countByMap(map);

            totalUserList.add(countTotalMapper);
            newUserList.add(newUserMapper);
        }
        return  UserReportVO.builder()
                .dateList(StringUtils.join(dateList,","))
                .newUserList(StringUtils.join(newUserList,","))
                .totalUserList(StringUtils.join(totalUserList,","))
                .build();


    }
}
